﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SD.Business;
using System.Globalization;
using SD.DataAccess;


namespace SD.Web.Views.Department
{
    public partial class DepartmentDetail : System.Web.UI.Page
    {
        /// <summary>
        /// The method is invoked when every page loaded
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadCountries();
                InitSaveMode();
            }
          
        }

       
       

        /// <summary>
        /// Loads the department.
        /// </summary>
        private void LoadDepartment()
        {
            int departmentId;
            if (int.TryParse(HiddenFieldDepartmentId.Value, out departmentId))
            {
                SD.DataAccess.Department department = DepartmentBUS.GetDepartmentById(departmentId);

                HiddenFieldDepartmentId.Value = department.DepartmentId.ToString(CultureInfo.CurrentCulture);
                HiddenFieldDepartmentName.Value = department.DepartmentName;
                TextBoxDepartmentName.Text = department.DepartmentName;

                if (department.Contact != null)
                {
                    Contact contact = department.Contact;
                    HiddenFieldContactId.Value = contact.ContactId.ToString(CultureInfo.CurrentCulture);
                    TextBoxLeadContact.Text = string.Format(CultureInfo.CurrentCulture, "{0} {1}", contact.FirstName,
                                                            contact.Surname);
                }

                if (department.Address != null)
                {
                    Address address = department.Address;
                    HiddenFieldAddressId.Value = address.AddressId.ToString(CultureInfo.CurrentCulture);
                    TextBoxAddressLine1.Text = address.AddressLine;
                    TextBoxAddressLine2.Text = address.AddressLine2;
                    TextBoxAddressLine3.Text = address.AddressLine3;
                    DropDownListNationCounty.SelectedValue = department.CountryId;
                    TextBoxPostcode.Text = address.Postcode;
                }

                if (department.BusinessType != null)
                {
                    BusinessType business = department.BusinessType;
                    HiddenFieldBusinessId.Value = business.BusinessId.ToString(CultureInfo.CurrentCulture);
                    TextBoxSICCode.Text = business.SICCode;
                    TextBoxTypeOfBusiness.Text = business.BusinessName;
                }

                TextBoxFullDescription.Text = department.FullDescription;
                TextBoxShortDescription.Text = department.ShortDescription;
                TextBoxFax.Text = department.Fax;
                TextBoxEmail.Text = department.Email;
                TextBoxCounty.Text = department.County;
                TextBoxTowVillageCity.Text = department.CityTown;
                TextBoxWebAddress.Text = department.WebAddress;
                TextBoxPhoneNumber.Text = department.PhoneNumber;
                HiddenFieldDirectorateId.Value = department.DirectorateId.ToString(CultureInfo.CurrentCulture);
            }
        }

        /// <summary>
        /// Loads the countries.
        /// </summary>
        private void LoadCountries()
        {
            DropDownListNationCounty.DataSource = CountryBUS.SelectCountries();
            DropDownListNationCounty.DataBind();
        }

        /// <summary>
        /// Inits the save mode.
        /// </summary>
        private void InitSaveMode()
        {
            int directorateId;
            if (int.TryParse(Request.Params["directorateId"], out directorateId))
            {
             
                    ButtonSave.Enabled = true;
                    ButtonSave.CommandName = "new";
                    HiddenFieldDirectorateId.Value = directorateId.ToString(CultureInfo.CurrentCulture);

                   // LoadDefaultValues();
                
            }

            int departmentId;
            if (int.TryParse(Request.Params["departmentId"], out departmentId))
            {
                SD.DataAccess.Department department = DepartmentBUS.GetDepartmentById(departmentId);
                if (department != null)
                {
                    ButtonSave.Enabled = true;
                    ButtonSave.CommandName = "edit";
                    cmdInActive.Visible = department.IsActive;

                    HiddenFieldDepartmentId.Value = departmentId.ToString(CultureInfo.CurrentCulture);
                    HiddenFieldDepartmentName.Value = department.DepartmentName;

                    LoadDepartment();
                }
            }
        }

         

        /// <summary>
        /// Handles the Command event of the ButtonSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void ButtonSave_Command(object sender, CommandEventArgs e)
        {
            var department = new SD.DataAccess.Department();
            department.DirectorateId = int.Parse(HiddenFieldDirectorateId.Value, CultureInfo.CurrentCulture);
            department.DepartmentName = TextBoxDepartmentName.Text;
            if (!HiddenFieldDepartmentName.Value.Equals(TextBoxDepartmentName.Text) &&
                DepartmentBUS.CheckDepartmentName(department.DepartmentName))
            {
                LabelCheckName.Text = "Department name must be unique";
                return;
            }

            int businessId;
            department.BusinessId = int.TryParse(HiddenFieldBusinessId.Value, out businessId) ? businessId : (int?)null;
            int addressId;
            department.AddressId = int.TryParse(HiddenFieldAddressId.Value, out addressId) ? addressId : (int?)null;
            int contactId;
            department.ContactId = int.TryParse(HiddenFieldContactId.Value, out contactId) ? contactId : (int?)null;
            department.ShortDescription = TextBoxShortDescription.Text;
            department.FullDescription = TextBoxFullDescription.Text;
            department.CityTown = TextBoxTowVillageCity.Text;
            department.County = TextBoxCounty.Text;
            department.CountryId = DropDownListNationCounty.SelectedValue;
            department.PhoneNumber = TextBoxPhoneNumber.Text;
            department.Fax = TextBoxFax.Text;
            department.Email = TextBoxEmail.Text;
            department.WebAddress = TextBoxWebAddress.Text;
            department.IsActive = true;
            department.DirectorateId = int.Parse(HiddenFieldDirectorateId.Value, CultureInfo.CurrentCulture);

            if (e.CommandName.Equals("edit"))
            {
                department.DepartmentId = int.Parse(HiddenFieldDepartmentId.Value, CultureInfo.CurrentCulture);
            }

            bool result = e.CommandName.Equals("new")
                              ? DepartmentBUS.InsertDepartment(department)
                              : DepartmentBUS.EditDepartment(department);

            if (result)
            {
                Response.Redirect(string.Format(CultureInfo.CurrentCulture,
                                                "~/Directorate/DirectorateDetails.aspx?directorateId={0}#DetailsPanel02",
                                                1));
            }
            else
            {
                DialogBox1.Error("Save Department successfully.");
            }
        }

        /// <summary>
        /// Handles the Command event of the LinkButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void LinkButton_Command(object sender, CommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "contact":
                   
                    ContactLookup1.Popup();
                    break;
                case "postcode":
                    AddressLookupControl1.Popup();
                   
                    HiddenFieldAddressId.Value = null;
                    TextBoxAddressLine1.Text = string.Empty;
                    TextBoxAddressLine2.Text = string.Empty;
                    TextBoxAddressLine3.Text = string.Empty;
                    TextBoxPostcode.Text = string.Empty;
                    break;
                case "business":
                    TypeOfBusinessLookup1.Popup();
                    break;
            }
        }

        /// <summary>
        /// Handles the Command event of the ButtonInactive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void cmdInactive_Command(object sender, CommandEventArgs e)
        {
            int departmentId;
            if (int.TryParse(HiddenFieldDepartmentId.Value, out departmentId))
            {
                DepartmentBUS.SetDepartmentStatus(departmentId, false);
                cmdInActive.Visible = false;
            }
        }

        /// <summary>
        /// Handles the Click event of the ButtonBack control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonBack_Click(object sender, EventArgs e)
        {
            int directorateId=1;
            if (int.TryParse(HiddenFieldDirectorateId.Value, out directorateId))
            {
                Response.Redirect(string.Format(CultureInfo.CurrentCulture,
                                                "~/Directorate/DirectorateList.aspx?directorateId={0}#DetailsPanel02",
                                                directorateId));
            }
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the RadioButtonListAddress control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void RadioButtonListAddress_SelectedIndexChanged(object sender, EventArgs e)
        {
            int directorateId=1;
            Directorate directorate = null;
            if (int.TryParse(HiddenFieldDirectorateId.Value, out directorateId))
            {
               
                if (directorate != null)
                {
                    Address address = RadioButtonListAddress.SelectedValue.Equals("Organization")
                                          ? directorate.Organization.Address
                                          : directorate.Address;

                    TextBoxAddressLine1.Text = address.AddressLine;
                    TextBoxAddressLine2.Text = address.AddressLine2;
                    TextBoxAddressLine3.Text = address.AddressLine3;
                    TextBoxPostcode.Text = address.Postcode;
                    HiddenFieldAddressId.Value = address.AddressId.ToString(CultureInfo.CurrentCulture);
                }
            }
        }
    }
}